// pages/RecommendSett/RecommendSett.js
// 参数1：currentA,I段电流定值
// 参数2：timeA,I段电流时间
// 参数3：currentB,II段电流定值
// 参数4：timeB,II段电流时间
// 参数5：capacity,该开关后总容量
// 参数6：masterNum,主干线已投

import WxValidate from '../../utils/WxValidate.js'
import Dialog from '@vant/weapp/dialog/dialog';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentA: "", //I段电流定值
        timeA: "", //I段电流时间
        currentB: "", //II段电流定值
        timeB: "", //II段电流时间
        currentC: "", //III段电流定值
        timeC: "", //III段电流时间
        masterNum: 0, //主干线已投数目
        capacity: "", //该开关后总容量
        messages: {
            currentA: "", //I段电流定值
            timeA: "", //I段电流时间
            currentB: "", //II段电流定值
            timeB: "", //II段电流时间
            currentC: "", //III段电流定值
            timeC: "", //III段电流时间
            masterNum: "", //主干线已投数目
            capacity: "", //该开关后总容量
        },
        resData: {
            current1: "",
            current2: "",
            current3: "",
            time1: 0,
            time2: 0,
            time3: 0,
            type1: "",
            type2: "",
            type3: "",
            reClosingTime:""
        }
    },
    //开关
    onChangeMasterNum({
        detail
    }) {
        this.setData({
            masterNum: detail
        });
    },
    //验证函数
    initValidate() {
        const rules = {
            currentA: {
                required: true,
                number: true,
                assistance: true,
                
            },
            timeA: {
                required: true,
                number: true,
                assistance: true
            },
            currentB: {
                required: true,
                number: true,
                assistance: true,
                currentBShouldLessThanCurrentA:true
            },
            timeB: {
                required: true,
                number: true,
                assistance: true,
                timeBShouldGreaterThanTimeA:true
            },
            currentC: {
                required: true,
                number: true,
                assistance: true,
                currentCShouldNotGreaterThanCurrentB: true
            },
            timeC: {
                required: true,
                number: true,
                assistance: true,
                timeCShouldNotLessThanTimeB: true
            },
            capacity: {
                required: true,
                number: true,
                assistance: true
            }
        }
        const messages = {
            currentA: {
                required: '请填写I段电流定值',
                number: '请输入有效正数'
            },
            timeA: {
                required: '请填写I段电流时间',
                number: '请输入有效正数'
            },
            currentB: {
                required: '请填写II段电流定值',
                number: '请输入有效正数'
            },
            timeB: {
                required: "请填写II段电流时间",
                number: '请输入有效正数'
            },
            currentC: {
                required: '请填写II段电流定值',
                number: '请输入有效正数'
            },
            timeC: {
                required: "请填写II段电流时间",
                number: '请输入有效正数'
            },
            capacity: {
                required: "请填写开关后总容量",
                number: '请输入有效正数'
            }
        }
        this.WxValidate = new WxValidate(rules, messages)
        let _this = this
        // 自定义验证规则
        this.WxValidate.addMethod('assistance', (value, param) => {
            return /^(\d+|\d+\.\d{1,2})$/.test(value)
        }, '正数并且最多保留两位小数')
        this.WxValidate.addMethod('currentBShouldLessThanCurrentA', (value, param) => {
            let { currentA } = _this.data
            return value * 1 < currentA * 1
        }, 'II段电流定值应当小于I段电流定值')
        this.WxValidate.addMethod('timeBShouldGreaterThanTimeA', (value, param) => {
            let { timeA } = _this.data
            return value * 1 > timeA * 1
        }, 'II段电流时间应当大于I段电流时间')
        this.WxValidate.addMethod('currentCShouldNotGreaterThanCurrentB', (value, param) => {
            let { currentB } = _this.data
            return value * 1 <= currentB * 1
        }, 'III段电流定值应当小于等于II段电流定值')
        this.WxValidate.addMethod('timeCShouldNotLessThanTimeB', (value, param) => {
            let { timeB } = _this.data
            return value * 1 >= timeB * 1
        }, 'III段电流时间应当大于等于II段电流时间')

    },
    //调用验证函数
    submitForm: function (e) {
        console.log('form发生了submit事件，携带的数据为：', e.detail.value)
        const params = e.detail.value
        this.setData({
            currentA: params.currentA * 1, //I段电流定值
            timeA: params.timeA * 1, //I段电流时间
            currentB: params.currentB * 1, //II段电流定值
            timeB: params.timeB * 1, //II段电流时间
            currentC: params.currentC * 1, //III段电流定值
            timeC: params.timeC * 1, //III段电流时间
            masterNum: params.masterNum * 1, //主干线已投数目
            capacity: params.capacity * 1, //该开关后总容量
        })
        //校验表单
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0]
            console.log(this.WxValidate.errorList)
            let errors = {}
            this.WxValidate.errorList.forEach((item) => {
                errors[item.param] = item.msg
            })
            // console.log(errors)
            this.setData({
                messages: errors
            })
            return false
        }
        this.setData({
            messages: {
                currentA: "", //I段电流定值
                timeA: "", //I段电流时间
                currentB: "", //II段电流定值
                timeB: "", //II段电流时间
                currentC: "", //II段电流定值
                timeC: "", //II段电流时间
                masterNum: "", //主干线已投数目
                capacity: "", //该开关后总容量
            }
        })
        this.submitData(params)
        // console.log(this.WxValidate)
        // this.showModal({
        //     msg: '提交成功'
        // })
    },
    submitData(data) {
        // console.log(this.data)

        this.setData({
            loading: true
        })
        let _this = this
        wx.request({
            url: 'https://gongxiangapi.haonanzhang.cn/api/electric/recommendSetting',
            data: {data: JSON.stringify(data)},
            method: 'POST',
            contentType: 'application/json',
            success({
                data
            }) {
                // let { data } = data
                _this.setData({
                    loading: false
                })
                if (typeof data.ret === 'undefined') {
                    _this.setData({resData:""})
                    Dialog.alert({
                        message: "抱歉,后台正在开小差,请稍后再试~",
                    })
                    return
                }
                _this.setData({resData:data.data})
                // console.log(_this.data.resData)
                Dialog.alert({
                        title: '返回结果'
                      }).then(() => {
                    // on close
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initValidate()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
